﻿/*
 * Copyright (c) 2010, Gavin Zhang
 * All rights reserved.
 * Licensed under the GPL License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Drawing;
using Rational.Test.Ft.Domain;
using Rational.Test.Ft.Domain.Net;
using Rational.Test.Ft.Script;

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base.ViewInfo;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System.Collections;
using Rational.Test.Ft.Vp;
using Rational.Test.Ft.Vp.Impl;

namespace DevExpress.Proxy
{
    public class XtraGridProxy : Media
    {
        public XtraGridProxy(NetTestDomainImplementation domain,
            IChannel channel,
            System.Object theTestObject)
            : base(domain, channel, theTestObject)
        {

        }

        public override Hashtable GetProperties()
        {
            System.Collections.Hashtable propertyTable = base.GetProperties();

            if (!propertyTable.Contains("RowCount"))
            {
                propertyTable.Add("RowCount", gridView().RowCount);
            }
            if (!propertyTable.Contains("ColumnCount"))
            {
                propertyTable.Add("ColumnCount", gridView().Columns.Count);
            }

            return propertyTable;
        }
        public override void Click(Subitem item)
        {
            if (item is Cell)
            {
                GridCellInfo gridCell = xtraGridCell((Cell)item);
                
                if (gridCell!=null)
                    Click(atPoint(gridCell.Bounds));
            }
        }

        public override Hashtable GetTestDataTypes()
        {
            Hashtable types = base.GetTestDataTypes();
            types.Add("contents", "Contents");
            return types;
        }
        public override ITestData GetTestData(string testDataType)
        {
            ITestData testData = null;
            switch (testDataType)
            {
                case "contents":
                    testData = createTestDataTable("contents","");
                    break;
            }
            return testData;
        }

        private ITestData createTestDataTable(string type, string description)
        {
            TestDataTable data = new TestDataTable("Table", description, type);

            if (type.Equals("contents"))
            {
                GridView winGrid = gridView();
                if (winGrid != null)
                {
                    int cols = winGrid.Columns.Count;
                    int viewableRowCount = winGrid.RowCount;
                    object[] objArray2 = new object[cols];
                    for (int k = 0; k < viewableRowCount; k++)
                    {
                        for (int n = 0; n < cols; n++)
                        {
                            object obj3 = null;
                            try
                            {
                                obj3 = winGrid.GetRowCellDisplayText(rowHandle(k), gridColumn(n));
                                debug.Trace("contents"+k + ""+ n + ": " + obj3);
                                if (obj3 != null)
                                {
                                    obj3 = obj3.ToString();
                                }
                            }
                            catch (Exception)
                            {
                            }
                            objArray2[n] = obj3;
                        }
                        data.Add(objArray2);
                    }
                    for (int m = 0; m < cols; m++)
                    {
                        GridColumn control4 = winGrid.GetVisibleColumn(m);

                        string name = string.Empty;
                        if (control4 != null)
                        {
                            name = control4.Caption;

                        }
                        if ((name != null) && !name.Equals(string.Empty))
                        {
                            debug.Trace("Header" + m + ": " + name);
                            data.SetColumnHeader(m, name);
                        }
                        else
                        {
                            data.SetColumnHeader(m, "");
                        }
                    }
                }
                data.AddComparisonRegion(TestDataTableRegion.AllCells());
                data.SetCompareBothByLeftRegions(true);
                return data;
            }


            throw new NotImplementedException();
        }
        private GridView gridView()
        {
            return (GridView)((GridControl)theTestObject).MainView;

        }

        private GridCellInfo xtraGridCell(Cell cell)
        {
            Index column = cell.GetColumn().GetIndex();
            Index row = cell.GetRow().GetIndex();

            debug.Trace("row: " + row.GetIndex());
            debug.Trace("col: " + column.GetIndex());
            GridCellInfo xtraGridCell = gridViewInfo().GetGridCellInfo(rowHandle(row), gridColumn(column).AbsoluteIndex);

            return xtraGridCell;
        }

        private static Point atPoint(Rectangle bounds)
        {
            debug.Trace("x: " + bounds.Location.X + bounds.Width / 2);
            debug.Trace("y: " + bounds.Location.Y + bounds.Height / 2);
            return new Point(bounds.Location.X+bounds.Width/2,
                bounds.Location.Y+bounds.Height/2);
        }

        private GridViewInfo gridViewInfo()
        {
            GridViewInfo info = (GridViewInfo)gridView().GetViewInfo();
            return info;
        }

        private int rowHandle(Index row)
        {
            return rowHandle(row.GetIndex());
        }

        private int rowHandle(int index)
        {
            debug.Trace("gridRow: " + index);
            return gridView().GetVisibleRowHandle(index);
        }
        private GridColumn gridColumn(Index column)
        {
            return gridColumn(column.GetIndex());
        }
        private GridColumn gridColumn(int index)
        {
            GridColumn gridColumn = gridView().GetVisibleColumn(index);
            debug.Trace("gridColumn: " + gridColumn);

            return gridColumn;
        }

    }
}
